
module ApplicationHelper
  
  def reg_http_host_port
    /[a-z0-9_\-]{1,5}:\/\/(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?/
  end

  def store_location
      session[:return_to] = request.url
  end

  def return_to
      session[:return_to]
  end

  def url_mask(url)
    url.gsub reg_http_host_port, 'http://[host]:[port]'
  end

  def group_path
    if params[:group_path]
      session[:group_path] = params[:group_path]
    elsif !session[:group_path]
      session[:group_path] = composites_group.path
    end
    composites_group.path_local
  end

  def group_mask
    if params[:group_mask]
      session[:group_mask] = params[:group_mask]
    elsif !session[:group_mask]
      session[:group_mask] = composites_group.path_mask
    end
    composites_group.path_mask
  end

  def include_in_group_path?(path)
    Dir[group_path].select{|p| path.include?(p)}.size > 0
  end

  def composites_group
    SOA::APP.new(session[:group_path],session[:group_mask])
  end

end
